<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <div class="inline-item">
          <el-input v-model="search.name" clearable style="width: 200px;" placeholder="请输入查询信息"/>
          <el-button @click="searchData" icon="el-icon-search" type="primary">查询</el-button>
          <el-button type="primary" @click="showCreateSpecDialog"><i class="fa fa-tag"></i> 添加规格</el-button>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col">

        <el-table
          :data="specs"
          border
          tooltip-effect="dark"
          v-loading="loading"
          ref="table"
        >

          <el-table-column
            label="ID" width="80"
          >
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>


          <el-table-column
            label="名称" width="160"
          >
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>

          <el-table-column
            label="显示名称" width="160"
          >
            <template slot-scope="scope">{{ scope.row.displayName }}</template>
          </el-table-column>

          <el-table-column
            label="值">
            <template slot-scope="scope">
              <el-tag v-for="item in scope.row.values" :key="item" style="margin-right: 5px;">
                {{item}}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column
            label="状态" width="90">
            <template slot-scope="scope">
              <div v-if="scope.row.isShow==1" class="el-alert el-alert--success is-center">
                显示
              </div>
              <div v-else class="el-alert el-alert--error is-center">
                已隐藏
              </div>
            </template>
          </el-table-column>


          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="doEdit(scope.$index,scope.row)">
                编辑
              </el-button>
              <el-button type="text" @click="changeShowStatus(scope.$index,scope.row)">

                <el-tooltip placement="top" v-if="scope.row.isShow==0">
                  <div slot="content">操作后该规则将出现在规格选择列表中</div>
                  <span>显示</span>
                </el-tooltip>

                <el-tooltip placement="top" v-else>
                  <div slot="content">操作后该规则将不会出现在规格选择列表中</div>
                  <span>隐藏</span>
                </el-tooltip>


              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="col h-pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
    <admin-spec-edit v-model="currentValue" title="规格管理" @created="createdSpec"
                     :show.sync="showCreateSpec">
    </admin-spec-edit>
  </div>
</template>
<style>

</style>
<script>
  import AdminSpecEdit from 'src/components/admin/goods/components/SpecEdit'
  import AdminService from 'src/service/admin'
  import TableMix from 'src/components/table-mix'

  export default {
    name: 'AdminSystemSpec',
    components: {AdminSpecEdit},
    mixins: [TableMix],
    data() {
      return {
        specs: [],
        showCreateSpec: false,
        searchKey: '',
        loading: false,
        search: {
          name: null,
          pagination: {
            total: 0,
            page: 1,
            limit: 10
          },
          orderBy: "is_show desc,id"
        },
        currentValue: null
      }
    },
    methods: {
      handleSizeChange: function (size) {
        this.pagination.limit = size
        this.getDatas()
      },
      searchData: function () {
        this.search.pagination.page = 1
        this.getDatas()
      },
      handleCurrentChange: function (page) {
        this.pagination.page = page
        this.getDatas()
      },
      getDatas: function () {
        const me = this
        me.loading = true
        AdminService.searchSpec(this.search).then(function (data) {
          if (data != null) {
            me.specs = data.datas
            me.pagination = data.pagination
          }
          me.loading = false
        })
      },
      changeShowStatus: function (index, data) {
        const me = this;
        AdminService.spec({
          id: data.id,
          isShow: data.isShow == 1 ? 0 : 1
        }).then(function (ds) {
          if (ds != null && ds.datas.length > 0) {
            me.$message.success('修改显示状态成功。')
          }
          data.isShow = data.isShow == 1 ? 0 : 1;
        })
      },
      doEdit: function (index, data) {
        this.currentValue = data;
        this.showCreateSpec = true
      },
      showCreateSpecDialog: function () {
        this.currentValue = {
          name: '',
          isShow: 1,
          values: []
        }
        this.showCreateSpec = true

      },
      createdSpec: function (data) {
        this.showCreateSpec = false
        this.getDatas()
      }
    },

    mounted: function () {
      this.getDatas();
    }
  }
</script>
